package m3;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConnectionFactory;

import java.util.Scanner;

public class Producer {
    public static void main(String[] args) throws Exception{
        //连接
        ConnectionFactory f = new ConnectionFactory();
        f.setHost("192.168.64.140");
//        f.setPort(5672);    //默认端口可以省略
        f.setUsername("admin");
        f.setPassword("admin");
        Channel c = f.newConnection().createChannel();

        //定义fanout类型交换机: logs
        //参数 1:交换机的名字  2:类型
        c.exchangeDeclare("logs", BuiltinExchangeType.FANOUT);

        //向logs交换机发送消息
        while (true){
            System.out.println("输入消息:");
            String s = new Scanner(System.in).nextLine();
            /*
            参数:
                1.交换机名字
                2.对于fanout交换机无效
                3.消息的其他属性设置
                4.消息数据byte[]数组
            */
            c.basicPublish("logs", "", null, s.getBytes());
        }

    }

}
